package com.android.leetcode;

import java.util.ArrayList;
import java.util.List;

/**
 * 8. 字符串转换整数 (atoi)
 * 请你来实现一个 myAtoi(string s) 函数，使其能将字符串转换成一个 32 位有符号整数（类似 C/C++ 中的 atoi 函数）。
 * 这种题目真的毫无意义
 * https://leetcode-cn.com/problems/string-to-integer-atoi/comments/
 */
class Solution8 {
    public static void main(String[] args) {
        String s = "-91283472332";
        int result = myAtoi(s);
        System.out.println(result);
    }

    public static int myAtoi(String s) {
        List<Character> characters = new ArrayList<>();
        boolean isPositive = true;
        boolean isHasSigned = false;
        for (int i = 0; i < s.length(); i++) {
            if (s.charAt(i) == " ".charAt(0)) {
                if (characters.size() > 0||isHasSigned) {
                    break;
                }
            } else if (s.charAt(i) == "-".charAt(0) || s.charAt(i) == "+".charAt(0)) {
                if (isHasSigned) break;
                if (characters.size() > 0) {
                    break;
                }
                if (s.charAt(i) == "-".charAt(0)) {
                    isPositive = false;
                }
                isHasSigned = true;
            } else if (s.charAt(i) >= 48 && s.charAt(i) <= 57) {
                characters.add(s.charAt(i));
            } else {
                break;
            }
        }

        String xString = "";
        for (int i = 0; i < characters.size(); i++) {
            xString = xString + characters.get(i);
        }
        int index=0;
        boolean isHasZero=false;
        for (int i = 0; i < xString.length(); i++) {
            if (xString.charAt(i)!="0".charAt(0)){
                index=i;
                break;
            }else {
                isHasZero=true;
            }
        }
        xString=xString.substring(index);
        if (xString.equals("")){
            xString="0";
        }
        if (index==0&&isHasZero){
            xString="0";
        }
        int positive = power(2, 31) - 1;
        int negative = -power(2, 31);
        String positiveString = String.valueOf(positive);
        String negativeString = String.valueOf(negative);
        negativeString = negativeString.substring(1);
        if (isPositive) {
            if (xString.length() > positiveString.length()) {
                return positive;
            } else if (xString.length() == positiveString.length()) {
                for (int i = 0; i < xString.length(); i++) {
                    if (Integer.valueOf(xString.charAt(i)) < Integer.valueOf(positiveString.charAt(i))) {
                        break;
                    } else if (Integer.valueOf(xString.charAt(i)) > Integer.valueOf(positiveString.charAt(i))) {
                        return positive;
                    }
                }
            }
        } else {
            if (xString.length() > negativeString.length()) {
                return negative;
            } else if (xString.length() == negativeString.length()) {
                for (int i = 0; i < xString.length(); i++) {                    //如果有一个数小于范围，就跳出循环
                    if (Integer.valueOf(xString.charAt(i)) < Integer.valueOf(negativeString.charAt(i))) {
                        break;
                    } else if (Integer.valueOf(xString.charAt(i)) > Integer.valueOf(negativeString.charAt(i))) {
                        //如果有一个数大于范围就返回0
                        return negative;
                    }
                }
            }
        }
        if (!isPositive) {
            xString = "-" + xString;
        }
        return Integer.valueOf(xString);
    }

    private static int power(int i, int j) {
        for (int w = 0; w < j - 1; w++) {
            i = i * 2;
        }
        return i;
    }
}